# -*- coding: utf-8 -*-
import re
import random
import MySQLdb

words = open("words.txt")
p = re.compile(r"^(\w+)\s\w+\.(.+)")
allist = []
for line in words:
    m = p.match(line)
    if m:
        allist.append([m.groups()[0], re.sub(r"\s\w+\.", '；', m.groups()[1])])
random.shuffle(allist)
print allist[37][1]

db= MySQLdb.connect(
        host='112.126.90.160',
        port = 3306,
        user='root',
        passwd='totoro',
        db ='magic',
        charset='utf8',
        )
cursor = db.cursor()
class_id = 42
class_name = "CET-4词汇"
word_count = 14

round_id = 2169
orders = 1
i = 1
for w in allist:
    if i == 1:
        round_name = "第"+str(orders)+"关"
        round_desc = class_name + round_name
        sql =  '''INSERT INTO totoro_round(round_name, round_desc, class_id, class_name, word_count,orders)
                    VALUES ('%s', '%s', '%d', '%s', '%d', '%d')''' % (round_name, round_desc, class_id, class_name, word_count,orders)
        cursor.execute(sql)
        db.commit()
        round_id +=1
        orders+=1
    word = w[0]
    word_zh = w[1]
    voice = 'yd-'+word
    sql = '''INSERT INTO word(round_id, round_name, class_id, class_name, word, word_zh, voice)
                VALUES('%d', '%s', '%d', '%s', '%s', '%s', '%s')''' % (round_id, round_name, class_id, class_name, word, word_zh, voice)
    cursor.execute(sql)
    db.commit()
    i +=1
    if i>14:
        i=1
db.close()
